package test;


import com.sun.glass.ui.Size;
import sun.awt.SunHints;

import javax.xml.ws.soap.Addressing;


public class HashTable {
    public LinkedListTest[]array;
    public int size;//当前存储元素个数
    public int maxImum;//阈值
//构造方法
    public HashTable (){
        this.array=new LinkedListTest[8];
        maxImum=(int) (array.length*0.75);
    }
    public HashTable(int arrayLength,double value){
        this.array=new LinkedListTest[arrayLength];
        this.maxImum= (int) (arrayLength*value);
    }
//增
    public void add(Object key, Object value){
        int indext = key.hashCode()%array.length;
        if (array[indext]==null){
            array[indext]=new LinkedListTest();
            array[indext].add(key,value);
            return;
        }
        array[indext].add(key,value);
        size++;
    }
//查   遍历所有元素 根据key查询value（未完成）
    public void showAll(){
        for(int i=0;i<array.length;i++){


            System.out.println("当前下标为"+i);
            if(array[i]==null){
                continue;
            }
            array[i].showAll();
        }
    }

//链表内部类
    class LinkedListTest{
        private Node headNode;

        private void add(Object key, Object Value){
            Node node =new Node(key,Value);

            if(headNode==null){
                headNode=node;
                return;
            }
            Node lastNode=headNode;
            while (lastNode.next==null){
                if(lastNode.key.equals(key)){
                    lastNode.value=Value;
                    return;
                }
                lastNode=lastNode.next;
            }
            lastNode.next = node;
        }
        private void showAll(){
            Node node =headNode;
            while(true){
                System.out.print(node.key+"--value="+node.value+"----");
                if (node.next==null){
                    break;
                }
                node=node.next;
            }
        }

        class Node{
            public Object key;
            public Object value;
            public int hash;
            public Node next;

            public Node(Object key, Object value) {
                this.key = key;
                this.value = value;
                hash=key.hashCode();
            }
        }
    }

}
